import { _decorator, Collider, Component, ITriggerEvent, Node, sp } from 'cc';
import { GameManager } from '../framework/GameManager';
import { Constant } from '../framework/Constant';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;

@ccclass('BulletProp')
export class BulletProp extends Component {

    private _propSpeed = 0.3;
    private _propSpeedX = 0.3;
    private _gameManager = null;

    onEnable() {
        const collider = this.getComponent(Collider);
        collider.on('onTriggerEnter', this._onTriggerEnter, this);
    }

    onDisable() {
        const collider = this.getComponent(Collider);
        collider.off('onTriggerEnter', this._onTriggerEnter, this);
    }

    start() {

    }

    update(deltaTime: number) {
        let pos = this.node.position;
        if (pos.x >= 15) {
            this._propSpeedX = this._propSpeed;
        } else if (pos.x <= -15) {
            this._propSpeedX = -this._propSpeed;
        }
        this.node.setPosition(pos.x + this._propSpeedX, pos.y, pos.z - this._propSpeed);
        pos = this.node.position;
        if (pos.z > 50) {
            PoolManager.instance().putNode(this.node);
        }
    }

    public show(gameManager: GameManager,speed: number) {
        this._propSpeed = speed;
        this._gameManager = gameManager;
    }

    private _onTriggerEnter(event: ITriggerEvent) {
        const name = event.selfCollider.node.name;
        if (name === 'bulletH') {
            this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_H);
        } else if (name === 'bulletS') {
            this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_S);
        } else {
            this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_M);
        }
        PoolManager.instance().putNode(this.node);
    }
}


